package Dao;





import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import util.DataUtil;







import model.*;


public class nhanVienDao {
	public static ArrayList<nhanVienModel> getAllNhanVien(){
		ArrayList<nhanVienModel> listNhanVien = new ArrayList<nhanVienModel>();
		try {
			String sql = "SELECT ID,Ten_Dang_Nhap,Ma_Vai_Tro,Ghi_Chu,Dia_Chi,Gioi_Tinh,So_Dien_Thoai FROM NHANVIEN";
			ResultSet rs = DataUtil.executeQuery(sql);
			while (rs.next()){
				nhanVienModel model = new nhanVienModel();
				model.setId(rs.getInt("ID"));
				model.setTendangnhap(rs.getString("Ten_Dang_Nhap"));
				model.setMavaitro(rs.getInt("Ma_Vai_Tro"));
				model.setGhichu(rs.getString("Ghi_Chu"));
				model.setDiachi(rs.getString("Dia_Chi"));
				model.setGioitinh(rs.getString("Gioi_Tinh"));
				model.setSodt(rs.getString("So_Dien_Thoai"));
				
				listNhanVien.add(model);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return listNhanVien;
	}
public static ArrayList<nhanVienModel> searchNhanVien(String ten){
		ArrayList<nhanVienModel> listNhanVien = new ArrayList<nhanVienModel>();
		
		try {
			String sql = "SELECT ID,Ten_Dang_Nhap,Ma_Vai_Tro,Ghi_Chu,Dia_Chi,Gioi_Tinh,So_Dien_Thoai FROM NHANVIEN WHERE Ten_Dang_Nhap LIKE '%" +ten+ "%'";
			ResultSet rs = DataUtil.executeQuery(sql);
			System.out.println("Result Set:"+rs.getRow());
			while (rs.next()){
				nhanVienModel model = new nhanVienModel();
				model.setId(rs.getInt("ID"));
				model.setTendangnhap(rs.getString("Ten_Dang_Nhap"));
				model.setMavaitro(rs.getInt("Ma_Vai_Tro"));
				model.setGhichu(rs.getString("Ghi_Chu"));
				model.setDiachi(rs.getString("Dia_Chi"));
				model.setGioitinh(rs.getString("Gioi_Tinh"));
				model.setSodt(rs.getString("So_Dien_Thoai"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return listNhanVien;
	}	
public static Boolean deleteNhanVien(nhanVienModel model){
	Boolean kq = false;
	try {
		String sql = "delete from NhanVien where ID = ?";
		PreparedStatement ps = DataUtil.getConnection().prepareStatement(sql);
		ps.setInt(1, model.getId());
		ps.executeUpdate();
		kq = true;
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return kq;
}
public static Boolean updateNhanVien( nhanVienModel model){
	Boolean kq = false;
	try {
		String sql = "update NhanVien set Ten_Dang_Nhap=?,Ma_Vai_Tro=?, Ghi_Chu=? , Dia_Chi=?, Gioi_Tinh=?,So_Dien_Thoai =? where ID=?";
		PreparedStatement ps = DataUtil.getConnection().prepareStatement(sql);
		ps.setString(1, model.getTendangnhap());
		ps.setInt(2, model.getMavaitro());
		ps.setString(3, model.getGhichu());
		ps.setString(4, model.getDiachi());
		ps.setString(5, model.getGioitinh());
		ps.setString(6, model.getSodt());
		
		ps.executeUpdate();
		kq = true;
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return kq;
}
public static Boolean insertNhanVien( nhanVienModel model){
	Boolean kq = false;
	
		
	return kq;
}

}
